package org.example.chat.controller;

import org.example.chat.api.CommonResult;
import org.example.chat.entity.FriendRelation;
import org.example.chat.entity.User;
import org.example.chat.service.IFriendRelationService;
import org.example.chat.service.IUserService;
import org.example.chat.utils.CommonUtils;
import org.example.chat.validate.groups.AddGroup;
import org.example.chat.vo.ContactInfoVo;
import org.example.chat.vo.ModifyUserHeadVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

/**
 * @author cheval
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;
    @Autowired
    private IFriendRelationService friendRelationService;

    @GetMapping("/list")
    public List<User> list() {
        return  userService.queryAll();
    }


    /**
     * 查询用户好友列表
     * @param username
     * @return
     */
    @GetMapping("/friend/list")
    public CommonResult<List<ContactInfoVo>> obtainFriendList(@RequestParam(required = true) String username) {
        List<ContactInfoVo> friends = friendRelationService.queryUserFriendList(username);
        return CommonResult.successNoMessage(friends);
    }


    /**
     * 添加好友
     * @param relation
     * @return
     */
    @PostMapping("/friend/add")
    public CommonResult addFriend(@Validated({AddGroup.class}) @RequestBody FriendRelation relation) {
        // 1. 添加好友关系
        int flag = friendRelationService.addRelation(relation);
        if (flag == 1) {
            return CommonResult.fail();
        } else if (flag == 2) {
            return CommonResult.fail("已经是好友关系");
        }
        // 2. 向被添加好友添加
        return CommonResult.success();
    }

    /**
     * 通过账号或手机号查找用户
     * @param key 手机号或账号
     * @return
     */
    @GetMapping("/friend/query")
    public CommonResult<List<User>> queryUserByAccountOrPhone(String key) {
        List<User> users = userService.queryUserByAccountOrPhone(key);
        return CommonResult.successNoMessage(users);
    }


    /**
     * 修改头像
     * @param modifyUserHeadVo
     * @return
     */
    @PostMapping("/modifyHead")
    public CommonResult modifyUserHeadImage(@Validated @RequestBody ModifyUserHeadVo modifyUserHeadVo) {
        boolean flag = userService.updateUserHeadImage(modifyUserHeadVo.getUserId(), modifyUserHeadVo.getImgPath(), modifyUserHeadVo.getBigImgPath());
        if (flag) {
            return CommonResult.successNoMessage();
        }
        return CommonResult.failNoMessage();
    }

    @GetMapping("/qrcode")
    public CommonResult<String> getUserQRCode(String username) {
        int width = 300;
        int height = 300;
        String qrcode = CommonUtils.genCodeToBase64String(username, width, height);
        return CommonResult.successNoMessage(qrcode);
    }
}
